<!--
 * @Author: Dorange.Wang
 * @Date: 2021-04-02 16:09:55
 * @LastEditors: wangming
 * @LastEditTime: 2021-12-06 16:21:26
 * @Description: file content
-->

<template>
  <el-dialog :visible="showVisible" center :show-close="false" width="800px">
    <div class="b g3 f16 tl" slot="title">
      <div class="ks-row-between pl15 pr15">
        <span>{{ "所有版本" }}</span>
        <span
          class="el-icon-close g9 f26 poi"
          @click="showVisible = false"
        ></span>
      </div>
    </div>

    <div class="pl30 pr30">
      <el-table
        class="mt10"
        :data="tableData"
        v-loading="loading"
        style="width: 100%"
        current-row-key="id"
        stripe
        border
      >
        <el-table-column prop="version" label="版本编号" width="180">
          <template slot-scope="scope">
            {{ scope.row.version | version
            }}{{ scope.row.activated ? "【已激活】" : ""
            }}{{ scope.row.published ? "【已发布】" : "" }}
          </template>
        </el-table-column>
        <el-table-column prop="description" label="版本描述" />
        <el-table-column prop="createDate" label="创建时间" width="180">
          <template slot-scope="scope">
            {{ scope.row.createDate | moment }}
          </template>
        </el-table-column>
        <el-table-column prop="actions" label="操作" width="180">
          <template slot-scope="scope">
            <span class="table-actionItem">
              <el-button type="text" @click="handleDesign(scope.row)"
                >设计</el-button
              >
            </span>
            <span v-if="!scope.row.activated" class="table-actionItem">
              <el-popconfirm
                title="确定要激活该状态机吗？"
                placement="top"
                @confirm="handleDelete(scope.row)"
              >
                <el-button class="link-danger" type="text" slot="reference"
                  >删除</el-button
                >
              </el-popconfirm>
            </span>
            <span class="table-actionItem">
              <el-dropdown
                @command="handleProcessCommand($event, scope.row)"
                trigger="click"
                placement="bottom"
                size="medium"
              >
                <el-button type="text"
                  >更多<i class="el-icon-arrow-down el-icon--right"></i
                ></el-button>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item
                    v-if="scope.row.activated === 0"
                    command="activate"
                    >激活</el-dropdown-item
                  >
                  <el-dropdown-item command="version"
                    >新建版本</el-dropdown-item
                  >
                  <el-dropdown-item command="copy">复制</el-dropdown-item>
                  <el-dropdown-item command="desc">版本描述</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </span>
          </template>
        </el-table-column>
      </el-table>
      <div class="tr mt24">
        <x-pagination
          :total="total"
          :pageSize.sync="pageData.pageSize"
          :pageNo.sync="pageData.pageNo"
          @search="loadData"
        />
      </div>
    </div>

    <div slot="footer" class="tr">
      <el-button @click="showVisible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import ProcessForm from "../processForm";
import { getHmstatemodelHistory,putHmstatemodelActivate,deleteHmstatemodelVersion } from '@/apis/paas/HmStateModelController.js'
export default {
  name: "HistoryModal",
  data() {
    return {
      loading: false,
      showVisible: false,
      pageData: {
        pageSize: 10,
        pageNo: 1,
      },
      tableData: [],
      total: 0,
      callback: () => {},
    };
  },

  methods: {
    viewForm(record = {}) {
      this._processKey = record.key;
      this.loadData();
      return this.showModel();
    },

    showModel() {
      this.showVisible = true;
      return {
        then: (callback) => {
          if (typeof callback === "function") {
            this.callback = callback;
          }
        },
      };
    },

    loadData() {
      this.loading = true;
      getHmstatemodelHistory({
        ...this.pageData,
        key: this._processKey,
      })
        .then((res) => {
          this.tableData = res;
          this.total = res.length;
        })
        .finally(() => {
          this.loading = false;
        });
    },

    handleProcessCommand(command, record) {
      if (command === "copy") {
        this.handleCopy(record);
      } else if (command === "activate") {
        this.handleActivate(record);
      } else if (command === "desc") {
        this.handleDesc(record);
      } else if (command === "version") {
        this.handleVersion(record);
      }
    },

    handleDesign(record) {
      const { origin, pathname } = window.location;
      const { id, key, name } = record;
      window.open(
        `${origin}${pathname}#/stateDesign?id=${id}&key=${key}&name=${name}`
      );
    },

    handleActivate(record) {
      putHmstatemodelActivate({
        id: record.id,
      }).then((res) => {
        this.loadData();
        this.callback();
        this.$message.success("操作成功");
      });
    },

    handleVersion(record) {
      ProcessForm.verForm({
        ...record,
        sourceId: record.id,
      }).then(() => {
        this.loadData();
        this.$message.success("新增成功");
      });
    },

    handleDelete(record) {
      deleteHmstatemodelVersion([record.id]).then((res) => {
        this.loadData();
        this.$message.success("操作成功");
      });
    },

    handleCopy(record) {
      const { id, key, name, description } = record;
      ProcessForm.copyForm({
        key: key + "_copy",
        name,
        description,
        sourceId: id,
      }).then(() => {
        // 更新页面数据
        this.callback();
        this.$message.success("操作成功");
      });
    },

    handleDesc(record) {
      ProcessForm.descForm(record).then(() => {
        this.loadData();
        // 仅当这条记录激活时（当前数据存在于页面列表中）更新页面数据
        if (record.activated === 1) {
          this.callback();
        }
        this.$message.success("操作成功");
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>
